//
//  WrokManSendController.m
//  operativeSide-2.0
//
//  Created by 王春程 on 2016/12/15.
//  Copyright © 2016年 王春程. All rights reserved.
//

#import "WrokManSendController.h"
#import "PostWorkManTableView.h"

@interface WrokManSendController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *searchTextField;
@property (weak, nonatomic) IBOutlet UIButton *searchBtn;
@property (strong, nonatomic)               PostWorkManTableView *tableView;
@property (strong, nonatomic)               RACDisposable *loadingDispose;
@end

@implementation WrokManSendController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}


- (void)viewDidLoad {
    //44+64 49
    WS(weakSelf);
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.searchTextField.delegate = self;
    
    [[self.searchTextField.rac_textSignal map:^id _Nullable(NSString * _Nullable value) {
        NSLog(@"%@",value);
        return @(value.length >= 2);
    }] subscribeNext:^(NSNumber *x) {
        NSLog(@"%@",x);
        if([x boolValue] == YES) {
            [weakSelf showLoading];
        }
    }];

    [[self rac_signalForSelector:@selector(textFieldShouldReturn:) fromProtocol:@protocol(UITextFieldDelegate)] subscribeNext:^(id  _Nullable x) {
        [weakSelf.searchTextField resignFirstResponder];
        NSLog(@"%@",x);
    }];
    
    
    [[self.searchBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        NSLog(@"开始网络请求");
    }];
    
    self.tableView = [[PostWorkManTableView alloc] initWithFrame:CGRectMake(0,108, VIEW_WIDTH, VIEW_HEIGHT - 108 - 49) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    
    
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"postWorkmanSendItem" object:nil] subscribeNext:^(NSNotification * _Nullable notification) {
        NSLog(@"跳转派件");
    }];
    
    
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"postWorkmanScan" object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        NSLog(@"跳转扫码");
    }];
    
}

//deley
- (void)showLoading {
    //上次信号还没处理，取消它(距离上次生成还不到1秒)
    [self.loadingDispose dispose];
    @weakify(self);
    self.loadingDispose = [[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [subscriber sendCompleted];
        return nil;
    }] delay:1] //延时一秒
                           subscribeCompleted:^{
                               @strongify(self);
                               NSLog(@"延时1S 开始请求");
                               self.loadingDispose = nil;
                           }];  
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.searchTextField resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
